package com.topnav.mc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.js.lib.mybitsplus.JsServiceImpl;
import com.js.lib.mybitsplus.page.JsPage;
import com.topnav.mc.admin.controller.FullScreenMoniter.ChartDataBean;
import com.topnav.mc.admin.dao.mapper.DeviceAlarmMapper;
import com.topnav.mc.admin.entity.DeviceAlarm;
import com.topnav.mc.admin.entity.bean.DeviceAlarmBean;
import com.topnav.mc.admin.entity.enums.DeviceAlarmCate;
import com.topnav.mc.admin.entity.enums.DeviceAlarmDispose;
import com.topnav.mc.admin.entity.enums.DeviceType;
import com.topnav.mc.admin.entity.enums.EnumModal;
import com.topnav.mc.admin.entity.param.DeviceAlarmParam;
import com.topnav.mc.admin.service.IDeviceAlarmService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.topnav.mc.base.mc.McServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: kenny
 * @Date: 2023/11/7 11:47
 */
@Service
public class DeviceAlarmServiceImpl extends McServiceImpl<DeviceAlarmMapper, DeviceAlarm, DeviceAlarmBean, DeviceAlarmParam>
    implements IDeviceAlarmService {
    @Autowired
    private DeviceAlarmMapper deviceAlarmMapper;

    @Override
    public boolean beforeUpdate(DeviceAlarm entity){
        entity.setUpdateTime(new Date());
        return true;
    }

    @Override
    public boolean beforeSave(DeviceAlarm entity){
        if (entity.getDispose() == null){
            entity.setDispose(DeviceAlarmDispose.UnDispose.getCode());
        }
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return true;
    }

    @Override
    public List<ChartDataBean> countGroupByCateList(@Param("ew") Wrapper queryWrapper){
        List<DeviceAlarmBean> list = deviceAlarmMapper.countGroupByCateList(queryWrapper);
        List<ChartDataBean> reslutList = new ArrayList<>();
        reslutList.add(new ChartDataBean().setIntId(1).setName("SOS").setValue(0));
        //围栏
        reslutList.add(new ChartDataBean().setIntId(2).setName("msg_fence").setValue(0));
        //人脸
        reslutList.add(new ChartDataBean().setIntId(3).setName("msg_face").setValue(0));
        //车辆
        reslutList.add(new ChartDataBean().setIntId(4).setName("车辆").setValue(0));

        //合并 为其它类型
        for(int i=0;i<list.size();i++){
            if (list.get(i).getAlarmCate() == 1){
                ChartDataBean chartDataBean145 = reslutList.get(0);
                chartDataBean145.setValue(chartDataBean145.getValue() + list.get(i).getTotal());
            }else if (list.get(i).getAlarmCate() == 2 || list.get(i).getAlarmCate() == 3){
                ChartDataBean chartDataBean2 = reslutList.get(1);
                chartDataBean2.setValue(chartDataBean2.getValue() + list.get(i).getTotal());
            }else if (list.get(i).getAlarmCate() == 4){
                ChartDataBean chartDataBean145 = reslutList.get(2);
                chartDataBean145.setValue(chartDataBean145.getValue() + list.get(i).getTotal());
            }else if (list.get(i).getAlarmCate() == 5){
                ChartDataBean chartDataBean145 = reslutList.get(3);
                chartDataBean145.setValue(chartDataBean145.getValue() + list.get(i).getTotal());
            }else if (list.get(i).getAlarmCate() == 6){
                continue;
            }
        }
        list=null;
        return reslutList;
    }
    /**
     * 按类型统计报警
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public Page<DeviceAlarmBean> countGroupByCatePage(Page<DeviceAlarmParam> page, Wrapper queryWrapper,Integer alarmCate) {
        Page<DeviceAlarmBean> page1 = deviceAlarmMapper.countGroupByCatePage(page,queryWrapper);
        List<DeviceAlarmBean> records = new ArrayList<>();

        for (DeviceAlarmCate deviceAlarmCate : DeviceAlarmCate.values()) {
            //是全部查还是只查某一个
            if (alarmCate == null || alarmCate == deviceAlarmCate.getCode()){
                DeviceAlarmBean deviceAlarmBean = new DeviceAlarmBean();
                deviceAlarmBean.setTotalCode(String.valueOf(deviceAlarmCate.getCode()));
                deviceAlarmBean.setTotalName(deviceAlarmCate.getDesc());
                deviceAlarmBean.setTotal(0);
                for(DeviceAlarmBean dab : page1.getRecords()){
                    if (dab.getAlarmCate() == deviceAlarmCate.getCode()){
                        deviceAlarmBean.setTotal(dab.getTotal());
                        break;
                    }
                }
                records.add(deviceAlarmBean);
            }
        }
        page1.setTotal(DeviceAlarmCate.values().length);
        page1.setRecords(records);
        return page1;
    }

    /**
     * 结果值设备类型描述
     * @param deviceAlarmBean
     */
    private void getPropertyForDesc(DeviceAlarmBean deviceAlarmBean){
        //报警类型
        for (DeviceAlarmCate deviceAlarmCate : DeviceAlarmCate.values()) {
            if (deviceAlarmBean.getAlarmCate() == deviceAlarmCate.getCode()){
                deviceAlarmBean.setAlarmCateDesc(deviceAlarmCate.getDesc());
                break;
            }
        }
    }

    /**
     * 结果值设备类型描述
     * @param deviceAlarmBeans
     */
    private void getPropertyForDescs(List<DeviceAlarmBean> deviceAlarmBeans){
        //报警类型
        for (DeviceAlarmBean deviceAlarmBean : deviceAlarmBeans) {
            this.getPropertyForDesc(deviceAlarmBean);
        }
    }


    @Override
    public List<EnumModal> getDeviceAlarmCate() {
        return DeviceAlarmCate.toList();
    }

    @Override
    public Page<DeviceAlarmBean> pageInfo(Page<DeviceAlarmParam> page , Wrapper<DeviceAlarmParam> queryWrapper){
        Page<DeviceAlarmBean> page1 = super.pageInfo(page,queryWrapper);
/*        if (!CollectionUtils.isEmpty(page1.getRecords())){
            this.getPropertyForDescs(page1.getRecords());
        }*/
        return page1;
    }
    @Override
    public List<DeviceAlarmBean> listInfo(Wrapper<DeviceAlarmParam> queryWrapper){
        List<DeviceAlarmBean> list = super.listInfo(queryWrapper);
/*        if (!CollectionUtils.isEmpty(list)){
            this.getPropertyForDescs(list);
        }*/
        return list;
    }

    @Override
    public DeviceAlarmBean getOneInfo(Wrapper<DeviceAlarmParam> queryWrapper, boolean throwEx){
        DeviceAlarmBean deviceAlarmBean = super.getOneInfo(queryWrapper,throwEx);
/*        if (deviceAlarmBean != null){
            this.getPropertyForDesc(deviceAlarmBean);
        }*/
        return deviceAlarmBean;
    }
}
